/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.parts;

import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Rafael
 */
public class IntegerTextField extends JTextField
{
    Document doc = new PlainDocument()
    {
        @Override
        public void insertString( int offs, String str, AttributeSet a ) throws BadLocationException
        {
            try
            {
                Integer.parseInt( str );
            }
            catch ( Exception e )
            {
                return;
            }
            
            super.insertString( offs, str, a );
        }
        
    };

    public IntegerTextField()
    {
        setDocument( doc );
    }

    public void setValue( int quantity )
    {
        setText( ""+quantity );
    }
    
    public int getValue()
    {
        return (!getText().isEmpty() ? Integer.parseInt( getText() ) : 0);
    }
}
